//
//  LGAgreementAlertView.m
//  LGSanofiPatient
//
//  Created by 1234 on 2022/9/14.
//  用户协议弹框

#import "LGAgreementAlertView.h"

#define baseWidth  (Screen_W-viewPix(75))

@interface LGAgreementAlertView()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UITextView *contentTF;
/**   */
@property (nonatomic , strong)UIButton *refuseBtn;
/**   */
@property (nonatomic , strong)UIButton *agreeBtn;

@end

@implementation LGAgreementAlertView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        [self showAgreementContent];
    }
    return self;
}

//拒绝
-(void)refuseBtnAction{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 0.001, 0.001);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//同意
-(void)agreeBtnAction{
    [[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"agreement"];
    [self refuseBtnAction];
    if (self.agreementAlertAction) {
        self.agreementAlertAction(@"continue");
    }
}


-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    NSString *urlStr = LGNSString(URL);
    if (self.agreementAlertAction) {
        if([urlStr isEqual:@"content://agreement"]){
            [self refuseBtnAction];
            self.agreementAlertAction(@"agreement");
        }else if ([urlStr isEqual:@"content://privacy"]){
            [self refuseBtnAction];
            self.agreementAlertAction(@"privacy");
        }
    }
    return YES;
}

-(void)showAgreementContent{
    NSString *content = [NSString stringWithFormat:@"感谢您选择%@，为了更好地保障您的个人权利，在使用我们的服务前，请务必审慎阅读《用户协议》和《隐私政策》的全部内容，同意并接受全部条款后方可开始使用我们的服务。",AppTitle];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:content];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];
    [attStr addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:LGFont(14)} range:NSMakeRange(0, content.length)];
    NSRange range1 = [content rangeOfString:@"《用户协议》"];
    NSRange range2 = [content rangeOfString:@"《隐私政策》"];
    [attStr addAttribute:NSLinkAttributeName value:@"content://agreement" range:range1];
    [attStr addAttribute:NSLinkAttributeName value:@"content://privacy" range:range2];
    self.contentTF.attributedText = attStr;
    self.contentTF.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithString:@"#429BFF"],NSFontAttributeName:LGFont(14)};
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.contentTF];
    [self.baseView addSubview:self.agreeBtn];
    [self.baseView addSubview:self.refuseBtn];
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 1000, 1000);
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(37), topBarHeight+viewPix(93), baseWidth, viewPix(310))];
        _baseView.transform = CGAffineTransformScale(_baseView.transform, 0.001, 0.001);
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 10;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"用户协议与隐私政策" colorString:@"#333333" textFont:LGFontWeight(18, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(0, viewPix(27), baseWidth, viewPix(28));
    }
    return _titleLabel;
}

-(UITextView *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextView alloc]initWithFrame:CGRectMake(viewPix(21), viewPix(73), baseWidth-viewPix(45), viewPix(120))];
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.font = LGFont(14);
        _contentTF.editable = NO;
        _contentTF.delegate = self;
    }
    return _contentTF;
}

-(UIButton *)agreeBtn{
    if(!_agreeBtn){
        _agreeBtn = [UIButton buttonWithTitle:@"同意" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _agreeBtn.frame = CGRectMake(viewPix(24), kMaxY(self.contentTF.frame)+viewPix(30), baseWidth-viewPix(48), viewPix(33));
        _agreeBtn.backgroundColor = [UIColor colorWithString:@"#429BFF"];
        _agreeBtn.cornerRidus = 5;
        [_agreeBtn addTarget:self action:@selector(agreeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeBtn;
}

-(UIButton *)refuseBtn{
    if(!_refuseBtn){
        _refuseBtn = [UIButton buttonWithTitle:@"不同意" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#666666" imageName:@""];
        _refuseBtn.frame = CGRectMake(viewPix(24), kMaxY(self.agreeBtn.frame)+viewPix(9), baseWidth-viewPix(48), viewPix(33));
        _refuseBtn.backgroundColor = [UIColor whiteColor];
        [_refuseBtn addTarget:self action:@selector(refuseBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _refuseBtn;
}

@end
